/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package escom.ipn.Despacho;

import escom.ipn.modelo.delegate.Agencia_ExternaDelegate;
import escom.ipn.modelo.delegate.IncidenteDelegate;
import escom.ipn.modelo.delegate.Incidente_AgenciaDelegate;
import escom.ipn.modelo.delegate.catalogoIncidenteDelegate;
import escom.ipn.modelo.dto.Agencia_Externa;
import escom.ipn.modelo.dto.IncidenteClass;
import escom.ipn.modelo.dto.Incidente_Agencia;
import escom.ipn.modelo.dto.catalogoIncidenteClass;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Daniel
 */
public class notificarAgencia extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
//-----------------------------------------SALVAR EN TABLA INCIDENTE_AGENCIA--------------------
            HttpSession sess = request.getSession(true);
            Incidente_Agencia ia = new Incidente_Agencia();
            Incidente_AgenciaDelegate iad = new Incidente_AgenciaDelegate();
            ia.setAGENCIA_EXTERNA_id_agencia(Integer.parseInt(request.getParameter("agencia")));
            ia.setINCIDENTE_id_incidente(Integer.parseInt(request.getParameter("incidente")));
            try {
                iad.crear(ia);
            } catch (SQLException ex) {out.println(ex.toString());
                Logger.getLogger(notificarAgencia.class.getName()).log(Level.SEVERE, null, ex);
            }
            
//-----------------------------------------OBTENER DATOS DE AGENCIA--------------------           
            Agencia_Externa agencia = new Agencia_Externa();
            Agencia_ExternaDelegate agenciadelegate = new Agencia_ExternaDelegate();
            try {
                agencia = agenciadelegate.mostrarAgencia_Externabyid(Integer.parseInt(request.getParameter("agencia")));
                
            } catch (SQLException ex) {out.println(ex.toString());
                Logger.getLogger(notificarAgencia.class.getName()).log(Level.SEVERE, null, ex);
            }
            
//-----------------------------------------OBTENER DATOS DE INCIDENTE--------------------
            IncidenteClass ic= new IncidenteClass();                
            IncidenteDelegate id= new IncidenteDelegate();
            catalogoIncidenteClass cic = new catalogoIncidenteClass();
            catalogoIncidenteDelegate delcic = new catalogoIncidenteDelegate();
            ic.setId_incidente(Integer.parseInt(request.getParameter("incidente")));
            try {
                ic = id.mostrarIncidentesId(ic);
            } catch (SQLException ex) {out.println(ex.toString());
                Logger.getLogger(notificarAgencia.class.getName()).log(Level.SEVERE, null, ex);
            }
            cic.setId(Integer.toString(ic.getCAT_TIPO_INCIDENTE_id_tipo_incidente())); 
            try {
                cic = delcic.mostrarIncidenteCatalogoIncidente(cic);
            } catch (SQLException ex) {out.println(ex.toString());
                Logger.getLogger(notificarAgencia.class.getName()).log(Level.SEVERE, null, ex);
            }
            
 //-----------------------------------------ENVIA EMAIL--------------------           
            Mail CorreoAgencia = new Mail();
            String correoDestinatario1=agencia.getEmail1();
            String correoDestinatario2=agencia.getEmail2();
            String asunto = cic.getIncidente() + ic.getId_incidente();
            int incidente = ic.getId_incidente();
            String textoCorreo = "Notificacion de incidente\n\n";
            textoCorreo += ic.getLugar()+"\n\n";
            textoCorreo += ic.getFecha()+"\n\n";
            
            CorreoAgencia.enviarMail(correoDestinatario1, correoDestinatario2, asunto, textoCorreo,incidente);
 
//-----------------------------------------MOSTRAR TELEFONOS-----------------------------------------
           response.sendRedirect("Despachador/informacionAgencias.jsp?agencia="+agencia.getNombre_agencia()+"&tel1="+agencia.getTelefono1()+"&tel2="+agencia.getTelefono2()); 
           //response.sendRedirect("Despachador/informacionAgencias.jsp");
            
            
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
